using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;


public class TranslateText : MonoBehaviour
{
    [Header("是否使用翻译")]
    public bool use_translate = true;

    [Header("翻译编号")]
    public string code;

    [Header("是否用gameObject.name自动填充code")]
    public bool use_auto_code = true;

    string last_language;

    public void translate_text()
    {
        if (!use_translate)
            return;

        if (last_language == TranslatorManager.instance.language)
        {
            return;
        }
        last_language = TranslatorManager.instance.language;

        Text old_text_comp = BdTools.get_component_in_obj_self_or_children<Text>(gameObject);

        TMP_Text new_text_comp = null;
        if (old_text_comp == null)
            new_text_comp = BdTools.get_component_in_obj_self_or_children<TMP_Text>(gameObject);

        // BdUtils.log("--- translate_text --- old text:", text_comp.text, ", _code:", code, BdUtils.is_null(code), BdUtils.get_type(code));
        BdUtils.assert(old_text_comp || new_text_comp, $"gameObject[{gameObject.name}] --- code[{code}]对应的`text_comp`为空?");

        string translated_text = TranslatorManager.instance.get_translated_text_by_code(code);

        if (!BdUtils.is_null(translated_text))
        {
            if (replace_listeners.Count > 0)
            {
                translated_text = replace_listeners[0](translated_text);
            }

            if (BdUtils.conform_template_match_format(translated_text))
            {
                // BdUtils.log($"=== ContainsTemplateSymbol! translated_text: {translated_text}");
                translated_text = BdUtils.replace_template_variables(translated_text, TranslatorManager.instance.template_variable_dc);
            }

            if (old_text_comp)
                old_text_comp.text = translated_text;
            else
                new_text_comp.text = translated_text;
        }
    }

    private void Start()
    {
        if (use_auto_code)
            code = BdUtils.is_null(code) ? gameObject.name : code;

        BdUtils.log($"is_null: {BdUtils.is_null(code)}, code: [{code}]");
        BdUtils.assert(code, $"gameObject[{gameObject.name}]的code为空!");

        var comp = GetComponent<TranslateText>();
        if (!TranslatorManager.instance.translate_comp_dc.ContainsKey(code))
            TranslatorManager.instance.translate_comp_dc.Add(code, comp);

        // add_template_pretreat(my_replace_template_pretreat);
    }

    private void OnDestroy()
    {
        if (TranslatorManager.instance.translate_comp_dc.ContainsKey(code))
            TranslatorManager.instance.translate_comp_dc.Remove(code);
    }


    public delegate string IReplaceListener(string template);
    // 监听列表, 在切换语言后触发
    // public static Dictionary<string, IReplaceListener> replace_listeners = new Dictionary<string, IReplaceListener>();
    public static List<IReplaceListener> replace_listeners = new List<IReplaceListener>();

    public void add_template_pretreat(IReplaceListener method)
    {
        // IReplaceListener listener1 = my_replace_template_pretreat;
        replace_listeners.Clear();
        replace_listeners.Add(method);
    }


    // Update is called once per frame
    void Update()
    {

    }
}
